package kr.saeildamil.androidstudty.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudty.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyListAdapter extends BaseAdapter {
	Context maincontext;
	LayoutInflater Inflater;
	ArrayList<MyItem>arSrc;
	int icontextLayout;
	
	public MyListAdapter(Context maincontext, int icontextLayout,
			ArrayList<MyItem> arSrc) {
		super();
		this.maincontext = maincontext;
		this.icontextLayout = icontextLayout;
		this.arSrc = arSrc;
		this.Inflater=(LayoutInflater) maincontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arSrc.size();
	}

	@Override
	public Object getItem(int position) {
		return arSrc.get(position).name;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertview, ViewGroup parent) {
		final int pos=position;
		if(convertview==null){
			convertview=Inflater.inflate(icontextLayout, parent, false);
		}
		ImageView img=(ImageView) convertview.findViewById(R.id.ch12_icontext_img);
		img.setImageResource(arSrc.get(position).icon);
		
		TextView tv=(TextView) convertview.findViewById(R.id.ch12_icontext_tv);
		tv.setText(arSrc.get(position).name);
		
		Button btn=(Button) convertview.findViewById(R.id.ch12_icontext_btn);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String str=arSrc.get(pos).name+"주문합니다.";
				Toast.makeText(maincontext, str, Toast.LENGTH_SHORT).show();
				
			}
		});
		return convertview;
		
	}

}
